import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import direct from '@dfeidao/atom-web/url/direct-to';

export default async function whoami(fd: IFeidaoAiBrowserComponent): Promise<{ userinfo: { usercode: string; user_name: string; productid: string; }, params: { productid?: string, staff_name?: string, staff_no?: string, headimg?: string } }> {
	// todo
	try {
		const res = await nodejs<{ userinfo: { usercode: string; user_name: string; productid: string; }, params: { productid: string, staff_name: string, staff_no: string } }>('atom/whoami_service', {});
		return res;
	} catch (error) {
		if (error.message === 'Could not get user auth info:undefined') {
			throw new Error(error.message);
		} else {
			const page_name = fd.data.url.match(/.*\/((.*?)\.html)/)[1];
			const params = fd.data.url.includes('?') ? fd.data.url.substr(fd.data.url.indexOf('?')) : '';
			const backurl = {
				backurl: './' + page_name + params
			};
			direct('./login.html', backurl);
			throw new Error(error.message);
		}
	}
}
